<?php
/**
 * Desc:
 * Date: 2019-11-04 17:35
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BaseModel extends Model
{
    /**
     * 错误信息
     */
    static protected $error = '';

    /**
     * Desc: 获取错误信息
     */
    static public function getErr()
    {
        return static::$error;
    }

    /**
     * Desc: 设置错误信息
     * @param $msg
     */
    static public function setErr($msg)
    {
        static::$error = $msg;
    }

    /**
     * Desc: 获取列表
     * @param array $param
     * @return array
     */
    static public function getPageList($param = [])
    {
        $where = isset($param['where']) ? $param['where'] : [];
        $orderBy = isset($param['orderBy']) ? $param['orderBy'] : '';
        $groupBy = isset($param['groupBy']) ? $param['groupBy'] : [];
        $field = isset($param['field']) ? $param['field'] : ['*'];
        $with = isset($param['with']) ? $param['with'] : [];
        $whereHas = isset($param['whereHas']) ? $param['whereHas'] : [];
        $whereDoesntHave = isset($param['whereDoesntHave']) ? $param['whereDoesntHave'] : [];
        $join = isset($param['join']) ? $param['join'] : [];
        $leftJoin = isset($param['leftJoin']) ? $param['leftJoin'] : [];

        array_key_exists('pageSize', $param) OR $param['pageSize'] = config('admin.pageSize');
        /*开始查询*/
        $res = self::select($field)
            ->where($where)
            /*关联查询*/
            ->when($with, function ($query) use ($with) {
                foreach ($with as $k => $v) {
                    $query->with($v);
                }
            })
            /*连表查询*/
            ->when($whereHas, function ($query) use ($whereHas) {
                foreach ($whereHas as $k => $v) {
                    $query->whereHas($v[0], $v[1]);
                }
            })
            ->when($whereDoesntHave, function ($query) use ($whereDoesntHave) {
                foreach ($whereDoesntHave as $k => $v) {
                    $query->whereDoesntHave($v[0], $v[1]);
                }
            })
            /*排序*/
            ->when($orderBy, function ($query) use ($orderBy) {
                foreach ($orderBy as $k => $v) {
                    $demo = explode(' ', $v);
                    $query->orderBy($demo[0], $demo[1]);
                }
            })
            /*分组*/
            ->when($groupBy, function ($query) use ($groupBy) {
                $query->groupBy($groupBy);
            })
            ->when($join, function ($query) use ($join) {
                foreach ($join as $v) {
                    $query->join($v[0], $v[1], $v[2]);
                }
            })
            ->when($leftJoin, function ($query) use ($leftJoin) {
                foreach ($leftJoin as $v) {
                    $query->leftJoin($v[0], $v[1], $v[2]);
                }
            })
            ->paginate($param['pageSize'])
            ->toArray();

        return ['total' => $res['total'], 'list' => $res['data']];
    }

    /**
     * Desc: 获取所有数据

     * @param array $param
     * @return array
     */
    static public function getAll($param = [])
    {
        $where = isset($param['where']) ? $param['where'] : [];
        $orderBy = isset($param['orderBy']) ? $param['orderBy'] : '';
        $field = isset($param['field']) ? $param['field'] : ['*'];
        /*开始查询*/
        $res = self::select($field)
            ->where($where)
            ->when($orderBy, function ($query) use ($orderBy) {
                foreach ($orderBy as $k => $v) {
                    $demo = explode(' ', $v);
                    $query->orderBy($demo[0], $demo[1]);
                }
            })
            ->get()
            ->toArray();
        return $res;
    }

    /**
     * Desc: 获取一条数据 不适用于有图片等需要关联的数据查询
     * @param int $id
     * @param array $field
     * @return object
     */
    static public function getOne($id = 0, $field = ['*'])
    {
        return self::select($field)
            ->where('id', $id)
            ->first();
    }

    /**
     * Desc: 单表添加
     * @param $params
     * @return mixed
     */
    static public function add($params)
    {
        try {
            $id = self::insertGetId($params);
            return ['id' => $id];
        } catch (\Exception  $ex) {
            self::setErr($ex->getMessage());
            return false;
        }
    }

    /**
     * Desc: 单表编辑
     * Date: 2019-11-08 11:47
     * @param $param
     * @return mixed
     */
    static public function edit($param)
    {
        try {
            self::where('id', $param['id'])->update($param);
            return "ok";
        } catch (\Exception  $ex) {
            self::setErr($ex->getMessage());
            return false;
        }
    }

    /**
     * Desc: 删除
     * @param $params
     * @return mixed
     */
    static public function del($params)
    {
        try {
            if (!array_key_exists('id', $params) || empty($params['id'])) {
                throw new \Exception('缺少参数id');
            }
            self::where('id', $params['id'])->delete();
            return "ok";
        } catch (\Exception  $ex) {
            self::setErr($ex->getMessage());
            return false;
        }
    }

    /**
     * Desc: 获取数据
     * @param array $param
     * @return mixed
     */
    static public function getStatistics($param = [])
    {
        $where = isset($param['where']) ? $param['where'] : [];
        $orderBy = isset($param['orderBy']) ? $param['orderBy'] : '';
        $groupBy = isset($param['groupBy']) ? $param['groupBy'] : [];
        $field = isset($param['field']) ? $param['field'] : ['*'];
        $with = isset($param['with']) ? $param['with'] : [];
        $whereHas = isset($param['whereHas']) ? $param['whereHas'] : [];
        $join = isset($param['join']) ? $param['join'] : [];

        return self::select($field)
            ->where($where)
            /*关联查询*/
            ->when($with, function ($query) use ($with) {
                foreach ($with as $k => $v) {
                    $query->with($v);
                }
            })
            /*连表查询*/
            ->when($whereHas, function ($query) use ($whereHas) {
                foreach ($whereHas as $k => $v) {
                    $query->whereHas($v[0], $v[1]);
                }
            })
            /*排序*/
            ->when($orderBy, function ($query) use ($orderBy) {
                foreach ($orderBy as $k => $v) {
                    $demo = explode(' ', $v);
                    $query->orderBy($demo[0], $demo[1]);
                }
            })
            /*分组*/
            ->when($groupBy, function ($query) use ($groupBy) {
                $query->groupBy($groupBy);
            })
            ->when($join, function ($query) use ($join) {
                foreach ($join as $v) {
                    $query->join($v[0], $v[1], $v[2]);
                }
            });

    }

    /**
     * 检测是否存在
     * @param $wheres
     * @return bool
     */
    public static function checkExists($wheres)
    {
        if (self::where($wheres)->exists()) {
            return true;
        }
        return false;
    }

    /**
     * 检测不存在
     * @param $wheres
     * @return bool
     */
    public static function checkNotExists($wheres)
    {
        if (self::query()->where($wheres)->doesntExist()) {
            return true;
        }
        return false;
    }
}